<template>
  <div class="login-page">
  <!-- 导航条 -->
  <van-nav-bar  title="注册"/>
  <!-- 中间表单 -->
  <van-form @submit="onSubmit">
  <van-field  name="username" v-model="username"  label="用户名"  placeholder="请输入用户名"
  :rules="[
    {required:true,message:'请填写用户名'},
    {pattern:/^\w{5,}$/,message:'用户名至少包含5个字符'}
  ]"
  />
  <van-field  type="password"  name="password"  v-model="password" label="密码"  placeholder="请输入密码"
  :rules="[
    {required:true,message:'请填写密码'},
    {pattern:/^\w{6,}$/,message:'用户名至少包含6个字符'}
  ]"
  />
  <div style="margin: 16px;">
    <van-button round block type="primary" native-type="submit">提交</van-button>
  </div>
</van-form>
<!-- 注册 -->
<router-link class="link" to="/login">有账号,去登录</router-link>
  </div>
</template>

<script>
// import request from '@/utils/request'
import { register } from '@/api/user'
export default {
  name: 'RegisterPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {

    async  onSubmit (value) {
      // try {
      this.$toast.loading({
        message: '正在注册中...',
        forbidClick: true
      })

      await register(value)
      this.$toast.success('注册成功')
      this.$router.replace('/login')
      // } catch (error) {
      //   this.$toast.fail('注册失败')
      // }
    }

  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
